package com.nexgo.payment.trans.operator.list;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.nexgo.payment.App;
import com.nexgo.payment.R;
import com.nexgo.payment.databases.OperatorInfo;
import com.nexgo.payment.databases.Repository;
import com.nexgo.payment.fragment.TextDialogFragment;
import com.nexgo.payment.trans.operator.add.OperatorAddActivity;
import com.nexgo.payment.trans.operator.edit.OperatorEditActivity;
import com.nexgo.payment.ui.BaseActivity;
import com.nexgo.payment.util.ToastUtils;
import com.nexgo.payment.util.UIUtils;
import com.nexgo.payment.widget.TitleBuilder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 操作员列表界面
 *
 * @author 谭忠扬-YuriTam
 * @date 2017年7月18日
 */
public class OperatorListActivity extends BaseActivity implements OperatorListContract.View {
    private static final String TAG = OperatorListActivity.class.getSimpleName();

    @BindView(R.id.lv_operate)
    ListView lvOperate;
    @BindView(R.id.tv_desc)
    TextView tvDesc;
    @BindView(R.id.tv_operator_no)
    TextView tvOperatorNo;

    private Context mContext;
    private Logger logger;
    private OperatorListPresenter mPresenter;

    private UserListAdapter mAdapter;
    private List<OperatorInfo> mOperatorList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_operator_list);
        ButterKnife.bind(this);

        mContext = this;
        logger = LoggerFactory.getLogger(TAG);
        new OperatorListPresenter(this, Repository.getInstance());

        initTitle();
    }

    //初始化标题栏
    private void initTitle() {
        new TitleBuilder(this)
                .setLeftImage(R.drawable.arrow_icon)
                .setRightText(getString(R.string.add))
                .setTitleText(getString(R.string.operator_list_title))
                .build();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.start();
    }

    @OnClick({R.id.title_iv_left, R.id.title_tv_right})
    public void onClicked(View view) {
        if (UIUtils.isDoubleClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.title_iv_left:
                finish();
                break;
            case R.id.title_tv_right:
                mPresenter.addOperatorInfo();
                break;
            default:
                break;
        }
    }

    @Override
    public void setPresenter(OperatorListPresenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public void showCurrentOperator(String operatorNo) {
        switch (operatorNo){
            case "00":
                tvDesc.setText(App.sContext.getString(R.string.admin_desc));
                break;
            case "99":
                tvDesc.setText(App.sContext.getString(R.string.manager_desc));
                break;
            default:
                tvDesc.setText(App.sContext.getString(R.string.operator_desc));
                break;
        }
        tvOperatorNo.setText(App.sContext.getString(R.string.operator_no) + "：" + operatorNo);
    }

    @Override
    public void showOperatorList(List<OperatorInfo> operatorList) {
        if (operatorList == null || operatorList.size() == 0) return;
        if (mAdapter == null) {
            mAdapter = new UserListAdapter(mContext);
            lvOperate.setAdapter(mAdapter);
        }
        this.mOperatorList = operatorList;
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void intent2AddOperator() {
        intent2ActivityForResult(OperatorAddActivity.class);
    }

    @Override
    public void intent2EditOperator(String operatorNo) {
        //跳转到编辑界面
        Bundle bundle = new Bundle();
        bundle.putString("operatorNo", operatorNo);
        intent2ActivityForResult(OperatorEditActivity.class, bundle);
    }

    @Override
    public void showTips(String message) {
        if (TextUtils.isEmpty(message)) return;
        ToastUtils.showToast(message);
    }

    @Override
    public void showDialog(String operatorNo) {
        TextDialogFragment dialogFragment = TextDialogFragment.newInstance();
        dialogFragment.setDialogType(TextDialogFragment.DIALOG_TYPE_WARNING);
        dialogFragment.setTitleText(getString(R.string.delete_operator));
        dialogFragment.setContentText(getString(R.string.delete_content_1) + operatorNo + getString(R.string.delete_content_2), Gravity.CENTER);
        dialogFragment.setPositionText(getString(R.string.confirm));
        dialogFragment.setNegativeText(getString(R.string.cancel));
        dialogFragment.setDialogListener(new TextDialogFragment.OnDialogListener() {
            @Override
            public void onCancel() {
            }

            @Override
            public void onConfirm() {
                mPresenter.deleteOperatorInfo(operatorNo);
            }
        });
        dialogFragment.show(getSupportFragmentManager(), null);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        logger.debug("requestCode = {},resultCode = {}", requestCode, resultCode);
        if (requestCode == 1000 && resultCode == 1000) {
            mPresenter.getOperatorList();
        }
    }

    //操作员管理适配器
    class UserListAdapter extends BaseAdapter {

        private Context mContext;
        private LayoutInflater mInflater;

        public UserListAdapter(Context mContext) {
            this.mContext = mContext;
            mInflater = LayoutInflater.from(mContext);
        }

        @Override
        public int getCount() {
            if (mOperatorList != null) return mOperatorList.size();
            return 0;
        }

        @Override
        public Object getItem(int position) {
            if (mOperatorList != null) return mOperatorList.get(position);
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder mHolder;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item_operator_list, null);
                mHolder = new ViewHolder(convertView);
                convertView.setTag(mHolder);
            } else {
                mHolder = (ViewHolder) convertView.getTag();
            }

            OperatorInfo info = mOperatorList.get(position);
            mHolder.tvId.setText(String.valueOf((position + 1)));
            mHolder.tvUserNo.setText(info.getOperatorNo());
            mHolder.tvEdit.setText(R.string.edit);
            mHolder.tvDelete.setText(R.string.delete);

            //编辑事件
            mHolder.tvEdit.setOnClickListener(v -> mPresenter.editOperatorInfo(info.getOperatorNo()));

            //删除事件
            mHolder.tvDelete.setOnClickListener(v -> mPresenter.confirmDelete(info.getOperatorNo()));

            return convertView;
        }

        class ViewHolder {

            @BindView(R.id.tv_id)
            TextView tvId;
            @BindView(R.id.tv_operator_no)
            TextView tvUserNo;
            @BindView(R.id.tv_edit)
            TextView tvEdit;
            @BindView(R.id.tv_delete)
            TextView tvDelete;

            public ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }
}
